package com.lagou.edu.service.impl;

import com.alibaba.druid.filter.AutoLoad;
import com.lagou.edu.annotations.MyAutowired;
import com.lagou.edu.annotations.MyComponent;
import com.lagou.edu.annotations.MyTranstional;
import com.lagou.edu.dao.AccountDao;
import com.lagou.edu.pojo.Account;
import com.lagou.edu.service.TransferService;
import com.lagou.edu.utils.ConnectionUtils;
import com.lagou.edu.utils.TransactionManager;

/**
 * @author 应癫
 */
@MyComponent("transferService")
@MyTranstional
public class TransferServiceImpl implements TransferService {

    @Override
    public String toString() {
        return "TransferServiceImpl{" +
                "accountDao=" + accountDao +
                '}';
    }

    @MyAutowired
    private AccountDao accountDao;


    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }


    @Override
    public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {
        Account from = accountDao.queryAccountByCardNo(fromCardNo);
        Account to = accountDao.queryAccountByCardNo(toCardNo);
        from.setMoney(from.getMoney() - money);
        to.setMoney(to.getMoney() + money);
        accountDao.updateAccountByCardNo(to);
//        int c = 1 / 0;
        accountDao.updateAccountByCardNo(from);
    }
}
